package com.shuang.graph6;

import java.util.*;
// 采用 DFS 进行搜索
 
public class Main {
 
    static int[][] dir = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; // 保存四个方向
 
    public static void dfs(int[][] heights, int x, int y, boolean[][] visited) {
        // 遇到边界或者访问过的点，直接返回
        if (visited[x][y]) return;
        // 满足条件，设置为true，标记访问
        visited[x][y] = true;
 
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
 
            // 超过边界
            if (nextX < 0 || nextX >= heights.length || nextY < 0 || nextY >= heights[0].length)
                continue;
 
            if (heights[nextX][nextY] >= heights[x][y]) {
                dfs(heights, nextX, nextY, visited);
            }
        }
    }
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
 
        int[][] heights = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                heights[i][j] = sc.nextInt();
            }
        }
 
        // 初始化两个二位boolean数组，代表两个边界
        boolean[][] first = new boolean[m][n];
        boolean[][] second = new boolean[m][n];
 
        // 从左右边界出发进行DFS
        for (int i = 0; i < m; i++) {
            dfs(heights, i, 0, first);
            dfs(heights, i, n - 1, second);
        }
 
        // 从上下边界出发进行DFS
        for (int j = 0; j < n; j++) {
            dfs(heights, 0, j, first);
            dfs(heights, m - 1, j, second);
        }
 
        // 当两个边界二维数组在某个位置都为true时，符合题目要求
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (first[i][j] && second[i][j]) {
                    res.add(Arrays.asList(i, j));
                }
            }
        }
 
        // 打印结果
        for (List<Integer> list : res) {
            for (int k = 0; k < list.size(); k++) {
                if (k == 0) {
                    System.out.print(list.get(k) + " ");
                } else {
                    System.out.print(list.get(k));
                }
            }
            System.out.println();
        }
    }
}